package com.zh.mediaservice.controller.User;
import com.zh.mediacommon.result.Result;
import com.zh.mediacommon.util.VerifyCodeUtil;
import com.zh.mediapojo.dto.UserDto;
import com.zh.mediapojo.dto.UserPasswordDTO;
import com.zh.mediapojo.dto.UserResetDTO;
import com.zh.mediapojo.entity.User;
import com.zh.mediapojo.entity.UserLog;
import com.zh.mediaservice.service.UserService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    private final UserService userService;
    private final RedisTemplate redisTemplate;
    /*
    * 注册用户
    * @param user
    * @return token
    * */
    @PostMapping("/register")
    public Result register(@RequestBody UserDto user){
        log.info("注册用户{}",user);
        String token=userService.insertUser(user);
        return Result.success(token);
    }
    /*
    * 用户登录
    * @param userLog
    * @return token
    * */
    @PostMapping("/log")
    public Result log(@RequestBody UserLog userLog , HttpServletRequest request){
        log.info("用户{}登录",userLog.getUsername());
        String token=userService.log(userLog.getUsername(),userLog.getPassword(),request);
        return Result.success(token);
    }
    /*
    * 修改密码
    * @param userPasswordDTO
    * @return
    * */
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody UserPasswordDTO userPasswordDTO){
        log.info("修改密码{}",userPasswordDTO);
        userService.updatePassword(userPasswordDTO);
        return Result.success("修改成功");
    }

    /*
      *
      *获取用户详细信息
     */
    @GetMapping
    public Result getUserInfo(){
        log.info("获取用户信息");
        User user=userService.getUserInfo();
        return Result.success(user);
    }
    /*
    * 获取验证码
    * @param phoneNumber
    *
    * */
    @GetMapping("/sendCode/{phoneNumber}")
    public Result sendCode(@PathVariable("phoneNumber") String phoneNumber){
        log.info("发送验证码{}",phoneNumber);
        String code= userService.sendCode(phoneNumber);
        return Result.success(code);
    }
    /*
    * 验证验证码
    * */
    @GetMapping("/verifyCode")
    public Result VerifyCode(@RequestParam String code,@RequestParam String phoneNumber){
        String code1 = (String) redisTemplate.opsForValue().get(phoneNumber);
        if (code.equals(code1))
            return Result.success("验证成功");
        return Result.error("验证码错误");
    }

    @PostMapping("/reset")
    public Result resetPassword(@RequestBody UserResetDTO userResetDTO){
        userService.resetPassword(userResetDTO.getNewPassword(),userResetDTO.getUsername());
        return Result.success("重置成功");
    }
}
